/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-02-03 10:59:34
 */

import {
  HIDE_APPLY_DIALOG,
  HIDE_APPROVE_DIALOG,
  SET_APPROVE_LIST,
  SET_ARC_LIST,
  SHOW_APPLY_DIALOG,
  SHOW_APPROVE_DIALOG,
  EDIT_APPROVE_ITEM
} from '../constants';
import { editItemById } from '@/utils/list';

export const RightApplyReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 打开查看申请弹窗
     */
    case SHOW_APPLY_DIALOG:
      return {
        ...state,
        applyRightDialog: {
          visible: true
        }
      };

    /**
     * @description: 关闭查看申请弹窗
     */
    case HIDE_APPLY_DIALOG:
      return {
        ...state,
        applyRightDialog: {
          visible: false
        }
      };

    /**
     * @description: 打开借阅详情弹窗
     */
    case SHOW_APPROVE_DIALOG:
      return {
        ...state,
        approveDialog: action.data
      };

    /**
     * @description: 关闭借阅详情弹窗
     */
    case HIDE_APPROVE_DIALOG:
      return {
        ...state,
        approveDialog: {
          visible: false,
          data: null
        }
      };

    /**
     * @description: 设置我发起的借阅列表
     * @param {*} data 列表数据
     */
    case SET_APPROVE_LIST:
      return {
        ...state,
        approveList: action.data,
        approveLoading: false
      };

    /**
     * @description: 设置借阅单内文件列表
     * @param {any[]} list 借阅单内文件列表
     */
    case SET_ARC_LIST:
      return {
        ...state,
        activeArcList: action.list
      };
    /**
     * @description: 设置我发起的借阅列表
     * @param {*} data 列表数据
     */
    case EDIT_APPROVE_ITEM: {
      const { id, data } = action;
      return {
        ...state,
        approveList: editItemById(id, data, state.approveList)
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
